import pymysql
# 录入数据

try:
    con = pymysql.connect(host="localhost",user="root",password="root",db="job51")
    print(con)
    cursor = con.cursor()

    newData = {
        "name": 'aaa',
        'companyName': '',
        'detailHref': '',
        'salary': '',
        'area': '',
        'type1': '',
        'fuli': '',
        'nums': '',
        'type2': '',
        'code1': '',
        'code2': ''
    }

    # 录入数据语句  ,area,type1,fuli,nums,type2,code1,code2
    sql='''
    insert into job(name,companyName,detailHref,salary) values(%s,%s,%s,%s)
    '''

    file = open(r"Q:\shilu\data\jobs2.csv",'r')
    line = file.readline()

    while line:
        words = line.split(",")
        # print(words)
        newData['name'] = words[0]
        newData['companyName'] = words[1]
        newData['detailHref'] = words[2]
        newData['salary'] = words[3]
        cursor.execute(sql, (newData['name'], newData['companyName'], newData['detailHref'], newData['salary']))
        line = file.readline()
    # 提交事务
    con.commit()
    print("录入成功")
except Exception as e:
    print(e)
    con.rollback()
    print("录入失败")
finally:
    # 关闭数据库连接
    con.close()
